home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-07-21 | 7.9 KB | 309 lines | [TEXT/CWIE] |
- // File Object Handler for the WASTE Text Engine
- // by Michael F. Kamprath, kamprath@kagi.com
- // maintenance by John C. Daub, hsoi@eden.com
- //
- // v1.0, 12 March 1995
- //
- // v1.1, 1 April. Fixed a a bug with draging hard drives to a WASTE instance
- // and a bix with insert file objects via InsertFileRefFromFSSpec()
- //
- // v1.2, 28 March 1996. Minor touch-ups and updates. Added more precompiler directives.
- // Added compatability with WASTE 1.2a5.
- //
- // v1.2.2, 16 July 1996. Added compatability with WASTE 1.2 and CW9
- // Restructured installation routines
-
- #ifndef __ICONS__
- #include <Icons.h>
- #endif
- #ifndef __DRAG__
- #include <Drag.h>
- #endif
- #ifndef __LOWMEM__
- #include <LowMem.h>
- #endif
- #ifndef _WASTE_
- #include "WASTE.h"
- #endif
-
- #ifndef _WASTEOBJECTS_
- #include "WASTE_Objects.h"
- #endif
-
- #include "WE_hfs_Handler.h"
- #include "GetFileIcon.h"
- #include "SendFinderOpen.h"
-
- #ifndef true
- #define true 1
- #endif
-
- #ifndef false
- #define false 0
- #endif
-
- // Local Functions
- static void SizeHFSObject( Point *objectSize, HFSFlavor **theHFS );
-
-
- //
- // InstallHFSObject()
- // Installs the HFS Object handler into WASTE.
- //
-
- OSErr InstallHFSObject( WEReference theWE )
- {
- OSErr iErr;
-
- #ifdef __cplusplus
- static WENewObjectUPP newHFSUPP = NewWENewObjectProc(HandleNewHFS);
- static WEDisposeObjectUPP disposeHFSUPP = NewWEDisposeObjectProc(HandleDisposeHFS);
- static WEDrawObjectUPP drawHFSUPP = NewWEDrawObjectProc(HandleDrawHFS);
- static WEClickObjectUPP clickHFSUPP = NewWEClickObjectProc(HandleClickHFS);
- #else
- static WENewObjectUPP newHFSUPP = NULL;
- static WEDisposeObjectUPP disposeHFSUPP = NULL;
- static WEDrawObjectUPP drawHFSUPP = NULL;
- static WEClickObjectUPP clickHFSUPP = NULL;
-
- if ( newHFSUPP == NULL )
- newHFSUPP = NewWENewObjectProc(HandleNewHFS);
- if ( disposeHFSUPP == NULL )
- disposeHFSUPP = NewWEDisposeObjectProc(HandleDisposeHFS);
- if ( drawHFSUPP == NULL )
- drawHFSUPP = NewWEDrawObjectProc(HandleDrawHFS);
- if ( clickHFSUPP == NULL )
- clickHFSUPP = NewWEClickObjectProc(HandleClickHFS);
- #endif
-
- if ( newHFSUPP != NULL )
- iErr = WEInstallObjectHandler(flavorTypeHFS, weNewHandler, (UniversalProcPtr)newHFSUPP, theWE);
- else
- iErr = weUnknownObjectTypeErr;
- if (iErr) return(iErr);
-
- if ( disposeHFSUPP != NULL )
- iErr = WEInstallObjectHandler(flavorTypeHFS, weDisposeHandler, (UniversalProcPtr)disposeHFSUPP, theWE);
- else
- iErr = weUnknownObjectTypeErr;
- if (iErr) return(iErr);
-
- if ( drawHFSUPP != NULL)
- iErr = WEInstallObjectHandler(flavorTypeHFS, weDrawHandler, (UniversalProcPtr)drawHFSUPP, theWE);
- else
- iErr = weUnknownObjectTypeErr;
- if (iErr) return(iErr);
-
- if ( clickHFSUPP != NULL )
- iErr = WEInstallObjectHandler(flavorTypeHFS, weClickHandler, (UniversalProcPtr)clickHFSUPP, theWE);
- else
- iErr = weUnknownObjectTypeErr;
- if (iErr) return(iErr);
-
- return(noErr);
- }
-
- //
- // New Object Handler for HFS Objects
- //
- pascal OSErr HandleNewHFS(Point *defaultObjectSize,WEObjectReference objectRef)
- {
- HFSFlavor **theHFS = (HFSFlavor**)WEGetObjectDataHandle(objectRef);
-
- // First size the object as WASTE requires
- SizeHFSObject( defaultObjectSize, theHFS);
-
- // Set the object refcon to 0. This is for safety
- // as later we use the refcon to store the icon handle
- // for this object.
- WESetObjectRefCon( objectRef, 0 );
-
- return(noErr);
- }
-
- //
- // Dispose Object handler for HFS Objects
- //
- pascal OSErr HandleDisposeHFS(WEObjectReference objectRef )
- {
- Handle theHFS = WEGetObjectDataHandle(objectRef);
- Handle iconCache = (Handle)WEGetObjectRefCon( objectRef );
-
- // If the object has an icon handle loaded, dispose of it.
- if (iconCache != 0)
- {
- DisposeIconSuite( iconCache, true );
- }
-
- // Dispos of the HFS data
- DisposeHandle(theHFS);
-
- return(MemError());
- }
-
- //
- // Draw Object Handler for HFS objects
- //
-
- pascal OSErr HandleDrawHFS(Rect *destRect, WEObjectReference objectRef )
- {
- HFSFlavor **theHFS = (HFSFlavor**)WEGetObjectDataHandle(objectRef);
- Handle iconCache = (Handle)WEGetObjectRefCon( objectRef );
- FontInfo fInfo;
- GrafPtr thePort;
- Rect iconRect;
- OSErr iErr;
- short oldTextFont, oldTextSize, oldTextFace;
- short sysTextFont = LMGetSysFontFam();
- short sysTextSize = LMGetSysFontSize();
-
- // if the object's refcon did not contain an icon handle, load one.
- if (iconCache == 0)
- {
- HLockHi( (Handle)theHFS );
- if ( ((*theHFS)->fileCreator=='MACS')&&((*theHFS)->fileType=='fold') )
- iErr = GetIconSuite(&iconCache,genericFolderIconResource,svAllAvailableData);
- else if ( ((*theHFS)->fileCreator=='MACS')&&((*theHFS)->fileType=='disk') )
- {
- iErr = GetIconSuite(&iconCache,genericHardDiskIconResource,svAllAvailableData);
- }
- else
- {
- iErr = GetFileIcon( &(*theHFS)->fileSpec, svAllAvailableData, &iconCache );
-
- if (iErr)
- iErr = GetIconSuite(&iconCache,genericDocumentIconResource,svAllAvailableData);
- }
- HUnlock( (Handle)theHFS );
- if (iErr)
- return(iErr);
- WESetObjectRefCon( objectRef, (long)iconCache );
- }
-
- // Determine the icon's rectangle and plot it
- SetRect( &iconRect, (destRect->right + destRect->left)/2 - 16, destRect->top,
- (destRect->right + destRect->left)/2 + 16, destRect->top + 32 );
- iErr = PlotIconSuite(&iconRect,(IconAlignmentType)(atVerticalCenter + atHorizontalCenter),
- (IconTransformType)ttNone, iconCache);
-
- // Draw the file's title. First save old font info for port.
- GetPort(&thePort);
- oldTextFont = thePort->txFont;
- oldTextSize = thePort->txSize;
- oldTextFace = thePort->txFace;
-
- TextFont( sysTextFont );
- TextSize( sysTextSize );
- TextFace( 0 );
- GetFontInfo(&fInfo);
-
- MoveTo( destRect->left + 1, destRect->bottom - fInfo.descent - fInfo.leading );
- DrawString( (*theHFS)->fileSpec.name );
-
- TextFont( oldTextFont );
- TextSize( oldTextSize );
- TextFace( oldTextFace );
-
- return( iErr );
- }
-
- //
- // Click Handler for HFS Objects.
- // Will send the finder an open selection apple event when object is clicked.
- //
-
-
- pascal Boolean HandleClickHFS( Point hitPt,
- short modifiers,
- long clickTime,
- WEObjectReference objectRef)
- {
- #pragma unused (hitPt, clickTime)
-
- HFSFlavor **theHFS;
-
- if (modifiers & 0x0001) // look for double-clicks
- {
- theHFS = (HFSFlavor**)WEGetObjectDataHandle(objectRef);
-
- HLockHi( (Handle)theHFS );
- SendFinderOpenAE(&(*theHFS)->fileSpec);
- HUnlock( (Handle)theHFS );
-
- return(true);
- }
- else
- return(false);
- }
-
- //
- // SizeHFSObject()
- // Used to return the display size of an HFS object
- //
- static void SizeHFSObject( Point *objectSize, HFSFlavor **theHFS )
- {
- FontInfo fInfo;
- GrafPtr thePort;
- short oldTextFont, oldTextSize, oldTextFace;
- short strWidth, strHeight;
- short sysTextFont = LMGetSysFontFam();
- short sysTextSize = LMGetSysFontSize();
-
- GetPort(&thePort);
- oldTextFont = thePort->txFont;
- oldTextSize = thePort->txSize;
- oldTextFace = thePort->txFace;
-
- TextFont( sysTextFont );
- TextSize( sysTextSize );
- TextFace( 0 );
-
- strWidth = StringWidth( (*theHFS)->fileSpec.name ) + 4;
- GetFontInfo(&fInfo);
- strHeight = fInfo.ascent + fInfo.descent + fInfo.leading;
-
- TextFont( oldTextFont );
- TextSize( oldTextSize );
- TextFace( oldTextFace );
-
- objectSize->h = ( strWidth > 32 ) ? strWidth : 32 ;
- objectSize->v = 32 + strHeight + 1;
- }
-
- //
- // InsertFileRefFromFSSpec()
- // Inserts a file object into a WASTE instance.
- //
-
- OSErr InsertFileRefFromFSSpec( FSSpec *theFile, WEReference theWE )
- {
- FInfo fndrInfo;
- HFSFlavor **theHFS;
- Point theSize;
- OSErr iErr;
-
- theHFS = (HFSFlavor**)NewHandleClear( sizeof(HFSFlavor) );
- if (theHFS)
- {
- // First construct the HFSFlavor object from the FSSpec
- iErr = FSpGetFInfo(theFile,&fndrInfo);
- if (iErr)
- {
- DisposeHandle( (Handle)theHFS );
- return(iErr);
- }
- HLock( (Handle)theHFS );
- (*theHFS)->fileType = fndrInfo.fdType;
- (*theHFS)->fileCreator = fndrInfo.fdCreator;
- (*theHFS)->fdFlags = fndrInfo.fdFlags;
- (*theHFS)->fileSpec = (*theFile);
- HUnlock( (Handle)theHFS );
-
- SizeHFSObject( &theSize, theHFS );
-
- // Now insert it into the WEReference
- WEInsertObject( flavorTypeHFS, (Handle)theHFS, theSize, theWE );
- }
- return(noErr);
- }
-